package com.atguigu.service.impl;

import com.atguigu.entity.Course;
import com.atguigu.entity.vo.CourseQuery;
import com.atguigu.mapper.CourseMapper;
import com.atguigu.service.CourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public List<Map<String, Object>> getPageList(long current, long limit, CourseQuery courseQuery) {
        Map<String, Object> param = new HashMap<>();
        if (!StringUtils.isEmpty(courseQuery.getId())) {
            param.put("id", String.valueOf(courseQuery.getId()));
        }
        if (!StringUtils.isEmpty(courseQuery.getTitle())) {
            param.put("title", courseQuery.getTitle());
        }
        int count = courseMapper.queryCount(param);
        if (count > 0) {
            param.put("limit", limit);
            param.put("offset", (current-1)*limit);
            if (!StringUtils.isEmpty(courseQuery.getOrderBy())) {
                param.put("orderBy", courseQuery.getOrderBy());
                if (!StringUtils.isEmpty(courseQuery.getOrder())) {
                    param.put("order", courseQuery.getOrder());
                }
            }
            return courseMapper.queryForList(param);
        }
        return null;
    }

    @Override
    public List<Course> getPageList(Page<Course> page, CourseQuery courseQuery) {
        return courseMapper.queryList(page, courseQuery);
    }

    @Override
    public List<Course> getNoPageList(CourseQuery courseQuery) {
        return courseMapper.queryList(courseQuery);
    }
}
